<?php

	/**
	 * Workspace@ Engine application comment!
	 * 
	 * LICENSE
	 * This application is licensed under New BSD license.
	 * Please read complete LICENSE.txt inside of root folder for more details.
	 * 
	 * We wish you good building :) 
	 * 
	 * @category   Application
	 * @package    Bootstrap
	 * @author     Nevio Vesic <nevio@workspaceat.com>
	 * @copyright  2008-2009 Workspace@ group in association with Design strategist
	 */
	
	/**
	 * @see Zend_Config_Xml
	 */
	require_once 'Zend/Config/Xml.php';
	
	require_once 'Application/Configuration/Backend.php';
	
	/**
	 * Enter description here...
	 *
	 */
	class Engine_Plugins_Language extends Engine_Application_Abstract
	{
		/**
		 * Xml file extension
		 *
		 */
		const XML_EXT  = ".xml";
		
		/**
		 * Object of current lang page!
		 *
		 * @var Object[Zend_Config_Xml]
		 */
		protected static $ldata   = null;
		
		const LANGUAGE_BASEPATH   = "languages";
		
		public static $frontsystem = null;
		
		protected $db          = null;
		
		function __construct()
		{
			if (! self::$frontsystem )
			{
				self::$frontsystem = null;
			}
			
			$a = new Engine_Application_Configuration_Backend( );
			self::$frontsystem = $a ->getFrontendInstance();
			
			
			if ( self::$ldata )
			{
				self::$ldata = null;
			}
			
			if ( self::$system -> users -> languages -> override == false )
			{
				# this means that we need to load default language!

				
			}
			
			$a = self::$data['elements'];
			
			$i = 0;
			
			if ( $a[0] )
			{
				foreach ( $a as $items )
				{
					if ( @$a[$i+1] )
					{
						$b .= $items . DIRECTORY_SEPARATOR;
					}
					else
					{
						$b .= $items;
					}
					++$i;
				}
			}
			else 
			{
				$b = self::$system -> start -> page;	
			}
			
			$b = $b . self::XML_EXT;
			
			$path = "languages" . DIRECTORY_SEPARATOR . strtolower(self::$data["instance"]) . DIRECTORY_SEPARATOR . 
					self::$system -> language -> defaults . DIRECTORY_SEPARATOR . $b;
			
			if (! fopen ( $path, "r", true ) )
			{
				throw new Engine_Exception( "We can't find proper language file for this page! Please fix it!" );
			}
			self::$ldata = new Zend_Config_Xml( $path );
			
			# set database
			$this->db = $this->databaseInstance();
		}
		
		public function getFrontendLanguageBasePath()
		{
			return self::LANGUAGE_BASEPATH . DIRECTORY_SEPARATOR . "frontend" . DIRECTORY_SEPARATOR .
			self::$frontsystem -> language -> defaults . DIRECTORY_SEPARATOR;
		}
		
		public function addGroup( Zend_Config $data )
		{
			if ( strlen ($data[0]) < 3 )
			{
				return array (
					"status" => "false",
					"message" => "Language name can't be smaller than 3 characters!"
				);
			}
			
			if(! preg_match("[:alpha:]", $data[0] ) )
			{
				return array (
					"status" => "false",
					"message" => "Language name can't contain numbers or special characters!"
				);	
			}
			
			# 1.) Check if exists
			# 2.) Add new folder inside of frontend language
			# 3.) Add database row
			# 4.) Return message to the user
			
			
		}
		
		public function __get( $name )
		{
			return self::$ldata -> $name;
		}
	}

